package com.tyk.bookstore.front.payment.strategy.impl;

import com.tyk.bookstore.front.member.service.MemberApiService;
import com.tyk.bookstore.front.payment.strategy.PayAction;
import com.tyk.bookstore.front.payment.strategy.query.PayQuery;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Component;
import org.springframework.transaction.support.TransactionTemplate;

/**
 * @author tyk
 * @description LocalPayAction
 */
@Component
@RequiredArgsConstructor
public class LocalPayAction implements PayAction<PayQuery> {

    private final MemberApiService memberApiService;

    private final TransactionTemplate transactionTemplate;

    @Override
    public String pay(PayQuery query, Runnable callback) {
        transactionTemplate.executeWithoutResult(status -> {
            // 扣减会员账户
            memberApiService.deductAmount(query.getPrincipal(), query.getCertificate(), query.getAmount());
            // 执行回调
            callback.run();
        });
        // 商城余额支付不返回信息
        return "";
    }


}
